<jsp:useBean id="coord" class="dasregistry.coordBean" scope="session" />
<jsp:useBean id="register" class="dasregistry.registryBean"	scope="page" />
<jsp:directive.page import="org.biojava.dasobert.dasregistry.DasCoordinateSystem" />
<jsp:directive.page	import="org.biojava.dasobert.dasregistry.DasCoordSysComparator" />
<jsp:directive.page import="java.util.*" />
<jsp:directive.page import="java.io.*" />
<jsp:directive.page	import="org.biojava.services.das.registry.DasRegistrySql" />
<%@ include file="sangerheader.jsp" %>
<%
   String title="help on coordinate systems";
   String header= title;
   BeanFactory ctx=(new ClassPathXmlApplicationContext("SpringDAO.xml"));
		DasRegistrySql registry=(DasRegistrySql)ctx.getBean("registryBean");
%>

<%@ page import="org.biojava.services.das.dao.Role" %>
<%@ page import="org.biojava.services.das.dao.RegistryUser" %>
<%@ page import="org.biojava.services.das.registry.UserManager" %>

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
    <title><%
    		if (title == null) {
    		title = "DAS registration - server";
    	}
    	out.print(title);
    %></title>
    <link rel="stylesheet"    type="text/css"     href="stylesheet.css" />
    <link rel="shortcut icon" type="image/x-icon" href="favicon.ico" />
	<script type="text/javascript" src="dropdown.js" language="javascript" ></script>
    <script type="text/javascript" src="DynamicOptionList.js" language="javascript" ></script>
<%
  	email = (String) session.getAttribute("email");
  	user = new RegistryUser();
  	isAdmin = false;
  	if (email != null) {
  		UserManager um = new UserManager();
  		user = um.getUser(email);
  		if (Role.isAdmin(user)) {
  			isAdmin = true;
  		}
  	}
%>  

</head>
<body onload="initDynamicOptionLists();">
<%@ include file="pagebodystart.jsp" %>
<div id="main">

<h2>Coordinate Systems</h2>

<div id="table">

<form method="get" action="listServices.jsp" name="SBform">
<script>
var dol = new DynamicOptionList();

var coordAuth = new DynamicOptionList();
coordAuth.addDependentFields("organism","CSName","CSTypes");

dol.setFormName("SBform");

<%
List<DasCoordinateSystem> coords = dasRegistrySql.getAllCoordinateSystems();
Comparator dsc               = DasCoordSysComparator.fromString("name");
java.util.Collections.sort(coords,dsc);

Map orgs = new HashMap();

for ( int i=0; i< coords.size();i++) {
	
	DasCoordinateSystem cs = coords.get(i);
	
	String authority = cs.getName();
	
	if ( authority.equals("") )
		authority = "any";
	
	if ( (cs.getVersion() != null) && 
			(! cs.getVersion().equals("")) ) {
		authority += "_" + cs.getVersion();
	}

	Map om = new HashMap();
	
	
	// authority -> type
	Map at = new HashMap();
	at.put(authority,cs.getCategory());
	
	String organism = cs.getOrganismName();
	
	if ( organism.equals("")){
		organism = "any";
	}
	
	om.put("organism",organism);
	
	Map knowmOm = (Map) orgs.get(""+i);
	
	List taxis = (List)om.get("list");
	if ( taxis == null )
		taxis = new ArrayList();

	taxis.add(at);
	// check if akred
	
	
	om.put("list",taxis);
	
	orgs.put(""+i,om);	
				
}

//Set keys = orgs.keySet();
//List keyL = new ArrayList(keys);
//Collections.sort(keyL);
//Iterator iter = keyL.iterator();
Iterator iter = orgs.keySet().iterator();
StringBuffer buf  = new StringBuffer();
StringBuffer buf3 = new StringBuffer();

List knownOptions= new ArrayList();
while (iter.hasNext()){
	
	String index = (String)iter.next();
	
	Map orgM = (Map) orgs.get(index);

	String orgl = (String) orgM.get("organism");
	
	List csses = (List) orgM.get("list");	

	Iterator iter2 = csses.iterator();			

	%>coordAuth.forValue("<%=orgl%>").addOptions(<%
			
	if ( ! knownOptions.contains(orgl) ) {			
		buf.append("<option value=\""+ orgl +"\">"+orgl+"</option>");			
		knownOptions.add(orgl);
	}
	List knownAuth = new ArrayList();
	
	while (iter2.hasNext()) {
	
		Map m = (Map) iter2.next();
		Iterator iter3 = m.keySet().iterator();

		while (iter3.hasNext()){
			String authority = (String) iter3.next();		

			if ( ! knownAuth.contains(authority) ) {
			
				if (knownAuth.size() > 0)
					%>,<%
				knownAuth.add(authority);
				%>"<%=authority%>"<%	
				buf3.append("coordAuth.forValue(\""+orgl+"\").forValue(\""+authority+"\").addOptions(");			
				buf3.append("\""+m.get(authority)+"\"");
				buf3.append(");\n");
			}

		}
	}%>);
<%
}
out.print(buf3);
%>

</script>


<select name="organism" ><%=buf %></select>
<select name="CSName"><script language="javascript">coordAuth.printOptions("CSName")</script></select>
<select name="CSTypes"><script language="javascript">coordAuth.printOptions("CSTypes")</script></select>
<input type="hidden" name="cmd" value="find">
<input type="submit" value="display">
</form>
</div>



</div>
<%@ include file="sangerfooter.jsp"%>


